package com.flqy.baselibrary.utils;

import android.graphics.Bitmap;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.flqy.baselibrary.GlideApp;
import com.flqy.baselibrary.GlideRequest;
import com.flqy.baselibrary.widget.transform.RoundedCornersTransformation;


public class GlideHelper {

    private GlideHelper() {
    }
    /**
     * 加载有圆角的头像
     *
     * @param
     * @param
     */
    public static void loadAdvertImage(@NonNull String photoUrl, @NonNull ImageView image) {
        GlideApp.with(image.getContext()).load(photoUrl)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(image);
    }

    /**
     * 普通加载
     * @param photoUrl
     * @param image
     */
    public static void loadImage(@NonNull String photoUrl, @NonNull ImageView image) {
        GlideApp.with(image.getContext()).load(photoUrl)
                .into(image);
    }



    /**
     * 加载有圆角的头像
     *
     * @param imageView
     * @param url       图片地址
     */
    public static void loadImage(ImageView imageView, Object url,int placeImage,int errorImage,int radius) {
        GlideApp.with(imageView.getContext())
                .load(url)
                .placeholder(placeImage)
                .error(errorImage)
                .apply(new RequestOptions()
                        .transforms(new CenterCrop(), new RoundedCorners(radius)
                        ))
                .into(imageView);

    }
    /**
     * 加载顶部有圆角的图片
     *
     * @param imageView
     * @param url       图片地址
     */
    public static void loadRoundTopImage(ImageView imageView, Object url,int placeImage,int errorImage,int radius) {
        GlideApp.with(imageView.getContext())
                .load(url)
                .placeholder(placeImage)
                .error(errorImage)
                .apply(new RequestOptions()
                        .transforms(new CenterCrop(), new RoundedCornersTransformation(imageView.getContext(),radius,0, RoundedCornersTransformation.CornerType.TOP,false)
                        ))
                .into(imageView);

    }
  /**
     * 加载有圆角的头像
     *
     * @param imageView
     * @param url       图片地址
     */
    public static void loadRoundImage(ImageView imageView, Object url,int placeImage,int errorImage) {
        loadImage(imageView,url,placeImage,errorImage,10);
    }


    /**
     * 加载有圆角的头像
     *
     * @param imageView
     * @param url       图片地址
     */
    public static void loadRoundImage(ImageView imageView, Object url) {
        GlideApp.with(imageView.getContext())
                .load(url)
                .apply(new RequestOptions()
                        .transforms(new CenterCrop(), new RoundedCorners(10)
                        ))
                .into(imageView);
    }





    public static void loadFlickrThumb(@NonNull final String photoUrl, String thumbnailUrl, @NonNull final ImageView image) {

        GlideRequest<Bitmap> thumbnail = null;

        if (thumbnailUrl != null) {
            thumbnail = GlideApp.with(image.getContext())
                    .asBitmap()
                    .load(thumbnailUrl)

                    .diskCacheStrategy(DiskCacheStrategy.ALL);
        }
        GlideRequest<Bitmap> glide = GlideApp.with(image.getContext())
                .asBitmap()
                .load(photoUrl)
                .dontAnimate();
        if (thumbnail != null) {
            glide.thumbnail(thumbnail);
        }
        glide.into(new GlideImageTarget(image));
    }

    public static void loadFlickrFull(@NonNull final String photoUrl, String thumbnailUrl, @NonNull final ImageView image,
                                      @Nullable final ImageLoadingListener listener) {

        GlideRequest<Bitmap> thumbnail = null;

        if (thumbnailUrl != null) {
            thumbnail = GlideApp.with(image.getContext())
                    .asBitmap()
                    .load(thumbnailUrl)
//                    .animate(ANIMATOR)
                    .diskCacheStrategy(DiskCacheStrategy.ALL);
        }

        GlideRequest<Bitmap> glide = GlideApp.with(image.getContext())
                .asBitmap()
                .load(photoUrl)

                .dontAnimate()
                .placeholder(image.getDrawable());
        if (thumbnail != null) {
            glide.thumbnail(thumbnail);
        }
        glide.diskCacheStrategy(DiskCacheStrategy.ALL)
                .listener(new RequestListener<Bitmap>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {

                        if (listener != null) {
                            listener.onFailed();
                        }

                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                        if (listener != null) {
                            listener.onLoaded();
                        }
                        return false;
                    }
                })
                .into(new GlideImageTarget(image));
    }


    public interface ImageLoadingListener {
        void onLoaded();

        void onFailed();
    }

}
